package com.shiftychess.view.components{
	import flash.desktop.NativeApplication;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.utils.setTimeout;
	
	
	public class TrayIcon{
		
		[Embed (source="tray_icon.gif")]
		public static const TrayIconGraphic:Class;

		[Embed (source="tray_icon_wink.gif")]
		public static const TrayIconWinkGraphic:Class;
		
		protected static var _icon:DisplayObject;
		protected static var _wink:DisplayObject;
				
		private static var _is_running:Boolean=false;
		public static function start():void{
			if(_icon==null){
				_icon=new TrayIconGraphic();	
				_wink=new TrayIconWinkGraphic();				
			}
			//trace("TrayIcon.start();");
			_is_running=true;
			showIcon();
		}
		
		public static function stop():void{
			_is_running=false;
			NativeApplication.nativeApplication.icon.bitmaps = [];
		}

		public static function wink():void{
			//trace("TrayIcon.wink();");
			if(!_is_running)return;
			var bmd:BitmapData=new BitmapData(16,16,true,0x00);
			bmd.draw(_wink);
			NativeApplication.nativeApplication.icon.bitmaps = [bmd];
			flash.utils.setTimeout(showIcon,300);
		}
		
		public static function showIcon():void{
			//trace("TrayIcon.showIcon();");
			if(!_is_running)return;
			var bmd:BitmapData=new BitmapData(16,16,true,0x00);
			bmd.draw(_icon);
			NativeApplication.nativeApplication.icon.bitmaps = [bmd];
			flash.utils.setTimeout(wink,1000+Math.round(Math.random()*3000));
		}

	}
}